package com.yungu.swift.base.service.impl;


import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.BaseDto;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminPageParam;
import com.yungu.swift.base.model.api.ApiPageParam;
import com.yungu.swift.base.service.BaseService;
import com.yungu.swift.utils.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by cuixiuyin on 2018/3/2
 */
public abstract class BaseServiceImpl<T extends BaseDto> implements BaseService<T> {

    private static final Logger log = LoggerFactory.getLogger(BaseServiceImpl.class);
    private static final String AGENT_UUID = "agentUuid";
    private static final String COMPANY_UUID = "companyUuid";

    protected abstract IMapper<T> getMapper();


    @Override
    public ResponseData<Boolean> add(T instance) {
        instance.setCreateOn(new Date());
        if (this.getMapper().add(instance) > 0) {
            return ResponseData.buildSuccessResponse("新增成功", Boolean.TRUE);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "新增失败");
    }

    @Override
    public ResponseData<Boolean> edit(T instance) {
        instance.setUpdateOn(new Date());
        if (this.getMapper().edit(instance) > 0) {
            return ResponseData.buildSuccessResponse("修改成功", Boolean.TRUE);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "修改失败");
    }

    @Override
    public ResponseData<Boolean> del(T instance) {
        if (this.getMapper().del(instance) > 0) {
            return ResponseData.buildSuccessResponse("删除成功", Boolean.TRUE);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "删除失败");
    }

    @Override
    public ResponseData<Boolean> del(String uuid, Class<T> tClass) {
        try {
            T t = tClass.newInstance();
            t.setUuid(uuid);
            return del(t);
        } catch (IllegalAccessException e) {
            log.error("IllegalAccessException, class name:{}", tClass.getName(), e);
        } catch (InstantiationException e) {
            log.error("InstantiationException, class name:{}", tClass.getName(), e);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "删除失败");
    }

    @Override
    public ResponseData<Integer> count(Map<String, Object> paramsMap) {
        return ResponseData.buildSuccessResponse("统计成功", this.getMapper().count(paramsMap));
    }

    @Override
    public ResponseData<T> get(Map<String, Object> paramMap) {
        List<T> list = this.getMapper().list(paramMap);
        if (list.size() > 0) {
            return ResponseData.buildSuccessResponse(list.get(0));
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "查询失败");
    }

    @Override
    public ResponseData<T> get(String uuid) {
        return get(MapUtils.build("uuid", uuid));
    }

    @Override
    public ResponseData<List<T>> list(Map<String, Object> paramMap) {
        return ResponseData.buildSuccessResponse(this.getMapper().list(paramMap));
    }

    @Override
    public <PageParam extends AdminPageParam> ResponseData<PageVo<T>> queryPage(PageParam pageParam) {
        PageBounds pageBounds = new PageBounds(pageParam.getPageNum(), pageParam.getPageSize());
        if (StringUtils.isNotEmpty(pageParam.getSortString())) {
            pageBounds.setOrders(Order.formString(pageParam.getSortString()));
        }
        PageList<T> pageList = this.getMapper().queryPage(MapUtils.convertObjToMap(pageParam), pageBounds);
        PageVo<T> pageVo = new PageVo(pageParam.getPageNum(), pageParam.getPageSize(), pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public <PageParam extends ApiPageParam> ResponseData<PageList<T>> queryPage(Map<String, Object> map, PageParam pageParam) {
        PageBounds pageBounds = new PageBounds(pageParam.getNowPage(), pageParam.getPageSize());
        PageList<T> pageList = this.getMapper().queryPage(map, pageBounds);
        return ResponseData.buildSuccessResponse(pageList);
    }

    @Override
    public <PageParam extends ApiPageParam> ResponseData<PageList<T>> queryPage(PageParam pageParam) {
        PageBounds pageBounds = new PageBounds(pageParam.getNowPage(), pageParam.getPageSize());
        PageList<T> pageList = this.getMapper().queryPage(MapUtils.convertObjToMap(pageParam), pageBounds);
        return ResponseData.buildSuccessResponse(pageList);
    }
}
